package com.steak.integral.execute;

import com.steak.common.domain.dataobject.tcc.Integral;
import com.steak.common.domain.dto.IntegralDTO;
import com.steak.common.result.Response;
import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * @author 刘牌
 * @Title:
 * @date 2022/3/116:44
 */
@Component
@AllArgsConstructor
public class IntegralExecute {

    private final JdbcTemplate jdbcTemplate;

    public Response execute(IntegralDTO integral) {
        String sql = "UPDATE integral SET integral = ( SELECT inters.integral FROM ( SELECT ( inte.integral + "+integral.getIntegral()+" ) integral FROM integral inte WHERE inte.user_id = "+integral.getUserId()+" ) inters) , freeze_integral = "+integral.getIntegral()+" WHERE user_id = "+integral.getUserId()+"";
        jdbcTemplate.update(sql);
        return new Response(200,"increase integral success",null);
    }

    public Response updateFreezeIntegralToZeroByUserId(Integer userId) {
        System.out.println("updateFreezeIntegralToZeroByUserId -----------------------");
        String sql = "update integral SET freeze_integral = 0 WHERE user_id = "+userId+"";
        jdbcTemplate.update(sql);
        return new Response(200,"update integral to zero success",null);
    }

    public Response updateIntegralBalance(IntegralDTO integralDTO) {
        String sql = "UPDATE integral SET integral = ( SELECT inters.integral FROM ( SELECT ( inte.integral - "+integralDTO.getIntegral()+" ) integral FROM integral inte WHERE inte.user_id = "+integralDTO.getUserId()+" ) inters) , freeze_integral = 0 WHERE user_id = "+integralDTO.getUserId()+"";
        jdbcTemplate.update(sql);
        return new Response(200,"recovery integral success",null);
    }
}
